    //
//  UIGroupViewController.m
//  Nav
//
//  Created by hisun on 11-11-11.
//  Copyright 2011 HISUNSRAY.wangming. All rights reserved.
//

#import "UIShowTimeObjListViewController.h"


@implementation UIShowTimeObjListViewController

@synthesize TimeObjList,optType,curID;

- (id)initWithTimeObjList:(NSMutableArray*)timeobj showname:(NSString*) showname{
    if (self = [super init]) {
        self.TimeObjList = timeobj;
        self.title  = showname;
    }
    return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    curID=0;
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[TimeObjList release];
    [super dealloc];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* dict;
    dict = [self.TimeObjList objectAtIndex:indexPath.row];
    if (dict)
    {
        NSString * type = [dict objectForKey:@"type"];
        NSString * refId = [dict objectForKey:@"refId"];
        NSString * personMark = [dict objectForKey:@"personMark"];
        NSString * name;
         NSString * given = [dict objectForKey:@"given"];
        if ([personMark length]<=0) {
            name = [MyClass getInstance].userName;
        }
        else
            name = [dict objectForKey:@"personShowName"];
        if ([type isEqualToString:@"plan"] || [type isEqualToString:@"deed"])
        {
            UITimeObjViewController* view = [[UITimeObjViewController alloc] initWithNibName:@"UITimeObjViewController" bundle:nil TimeObj:refId objType:type PersonName:name given:given];
            [self.navigationController pushViewController:view animated:YES];
            [view release];
        }
    }
}
#pragma mark -
-(NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
	return [TimeObjList count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView
       cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString  *CellIdentifier = @"CellIdentifier";
	UITableViewCell		*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (nil == cell) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier: CellIdentifier ] autorelease];
        cell.showsReorderControl = YES;
	}
    
    {
        NSDictionary* dict = [self.TimeObjList objectAtIndex:indexPath.row];
        if (dict)
        {
            NSDate * beginTime = [dict objectForKey:@"begin"];
            NSDate * endTime = [dict objectForKey:@"end"];
            NSString * name = [dict objectForKey:@"name"];
            //            NSString * personMark = [dict objectForKey:@"personMark"];
            //            NSString * personShowName = [dict objectForKey:@"personShowName"];
            //            NSString * refId = [dict objectForKey:@"refId"];	
            NSString * type = [dict objectForKey:@"type"];
            
            cell.textLabel.font= [UIFont boldSystemFontOfSize:16];
            
            
            //NSString * given = [dict objectForKey:@"given"];
//            if ([type isEqualToString:@"plan"] || [type isEqualToString:@"deed"])
//            {
//                if ([given isEqualToString:@"true"]) {
//                    NSString* tmp;
//                    if ([[dict objectForKey:@"personShowName"] length]>6) {
//                        NSRange rage;
//                        rage.length=6;
//                        rage.location=0;
//                        tmp=[tmp substringWithRange:rage];
//                        tmp= [NSString stringWithFormat:@"%@...",tmp];
//                        name =[NSString stringWithFormat:@"%@/%@" ,[dict objectForKey:@"name"],tmp]; 
//                    }
//                    else
//                        name =[NSString stringWithFormat:@"来自%@的%@" ,[dict objectForKey:@"personShowName"],[dict objectForKey:@"name"]];   
//                    
//                }
//                else
//                    name =[NSString stringWithFormat:@"%@的%@",[dict objectForKey:@"personShowName"] ,[dict objectForKey:@"name"]];
//            }
//            else
            {
                name = [dict objectForKey:@"name"];
            }         
            if ([name length]>12) {
                NSRange rage;
                rage.length=12;
                rage.location=0;
                name=[name substringWithRange:rage];
                name= [NSString stringWithFormat:@"%@...",name];
            }

            cell.textLabel.text = name;
            NSDateFormatter *format = [[NSDateFormatter alloc] init];
            NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"];
            [format setLocale:enLocale];
            [enLocale release];

            [format setDateFormat:@"yyyy-MM-dd hh:mm"];
            //NSDate *dateTime = [format dateFromString:message];
            NSString * dateString1 = [format stringFromDate:beginTime];
            NSString * dateString2 = [format stringFromDate:endTime];
            [format release];

            {
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ --- %@" ,dateString1,dateString2];
            }
            BOOL isPink=NO;
            BOOL isBlue=NO;
            BOOL isBrown=NO;
            BOOL isOrange=NO;
            BOOL isGreen=NO;
            if ([type isEqualToString:@"plan"] && !isPink)
            {
                cell.imageView.image =  [UIImage imageNamed:@"pink.png"] ;
            }
            else if ([type isEqualToString:@"deed"] && !isBlue)
            {
                isBlue=YES;
                cell.imageView.image = [UIImage imageNamed:@"blue.png"];
                
            }
            else if ([type isEqualToString:@"birthday"]&& !isGreen)
            {
                isGreen =YES;
                cell.imageView.image = [UIImage imageNamed:@"green.png"];  
            }
            else if ([type isEqualToString:@"payday"]&& !isOrange)
            {
                isOrange= YES;
                cell.imageView.image = [UIImage imageNamed:@"orange.png"];
                
            }
            else if ([type isEqualToString:@"activity"]&& !isBrown)
            {
                isBrown= YES;
                cell.imageView.image = [UIImage imageNamed:@"brown.png"];
            }
        }
    }
	return cell;
}



@end
